from google.appengine.api import users
import unittest
import os

#from nose import tools

from models.api import *

def setup():
    os.environ['AUTH_DOMAIN'] = 'gmail.com'
    os.environ['USER_EMAIL'] = 'testplayer@gmail.com'
    os.environ['USER_ID'] = 'testplayer'
    
def switch_user(email, nickname):    
    os.environ['USER_EMAIL'] = email
    os.environ['USER_ID'] = nickname
    
def test_GameManager_delete():
    manager=GameManager()
    
    count=len(manager.list_games())
    
    game_id=manager.create_game("ABC Testing Game")

    assert len(manager.list_games()) == count+1, "Number of games should be increase when new game created"
    
    manager.delete_game(game_id)

    assert len(manager.list_games()) == count, "Number of games should decrease when game deleted"
    
    pass

def test_GameManager_new_game():
    manager=GameManager()
    
    switch_user("grabags@gmail.com", "grabags")    
    game_id=manager.create_game("DEF Testing Game")
    manager.player_ready(game_id, True)
    
    try:
        manager.join_game(game_id)
    except GameManagerException,  e:
        assert True
    else:
        assert False, "Attempt to join already joined game should raise and error"
    
    switch_user("boris@gmail.com", "boris")
    manager.join_game(game_id)
    manager.player_ready(game_id, True)

#    switch_user("grabags@gmail.com", "testplayer")    
#    try:
#        manager.start_game(game_id)
#    except GameManagerException, e:
#        assert True
#    else:
#        assert False, "Attempt to start game with < 3 players should fail"
    
    switch_user("phil@gmail.com", "phil")
    manager.join_game(game_id)
    manager.player_ready(game_id, True)

    try:
        manager.start_game(game_id)
    except GameManagerException, e:
        assert True
    else:
        assert False, "Attempt to start someone elses game should fail"
    
    switch_user("grabags@gmail.com", "testplayer")    
    manager.start_game(game_id)
    
    game_info=manager.game_info(game_id)
    assert game_info["round"]==1, "Round should be 1 for started game, got %d"% game_info["round"]
    
 
    
    
    

    